package per.srp.demo.carLink.Utils;

import com.alibaba.fastjson.JSONObject;

import java.lang.reflect.Field;

public class PackageObjectFromJSONObject {

    /**
     * 通过JSONObject，动态将信息封装到对象
     * @param jsonInfo
     * @param t
     * @param <T>
     * @return
     */
    public static  <T> T pack(JSONObject jsonInfo,Class<T> t) throws Exception{
        T obj = t.newInstance();
        Field[] fields = t.getDeclaredFields();
        for(Field eachf : fields){
            eachf.setAccessible(true);
            eachf.set(obj,jsonInfo.getObject(eachf.getName(),eachf.getType()));
        }
        return obj;
    }
}
